package edu.lingnan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.lingnan.pojo.MyOrder;
import edu.lingnan.service.MyOrderService;
import edu.lingnan.mapper.MyOrderMapper;
import edu.lingnan.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class MyOrderServiceImpl extends ServiceImpl<MyOrderMapper, MyOrder>
    implements MyOrderService{

    @Autowired
    private MyOrderMapper myOrderMapper;

    @Autowired
    private ProductOrderService productOrderService;


    @Override
    public List<MyOrder> selectAllMyOrder(Long id) {
        return myOrderMapper.selectAllMyOrder(id);
    }

    @Override
    public List<MyOrder> selectOrderByOrderId(Long id) {
        return myOrderMapper.selectOrderByOrderId(id);
    }

    @Override
    public List<MyOrder> selectOrderByStoreId(Long id) {
        return myOrderMapper.selectOrderByStoreId(id);
    }

    @Override
    public int deleteOrderByOrderId(Long id) {
        //先删除商品订单关系表，再删除订单表
        productOrderService.deleteProductOrderByOrderId(id);
        return myOrderMapper.deleteOrderByOrderId(id);
    }

    @Override
    public void addOrder(MyOrder order,Long productId) {
        myOrderMapper.addOrder(order);
        int row = productOrderService.selectProductOrderByProductId(productId);
        if (row == 0) {
            //如果数据库中没有该商品订单，插入新的一条商品订单记录
            productOrderService.addProductOrder(order, productId);
        } else {
            //如果数据库中有该商品记录，则在原有product_number数据上加一
            productOrderService.updateProductOrderNumber(productId);
        }

    }
}




